import { of, map, filter, scan, fromEvent, throttleTime, debounceTime } from 'rxjs';

// 循环
of(1, 2, 3)
  .pipe(map((x) => x * x))
  .pipe(filter((x) => x % 2 !== 0))
  .subscribe((v) => console.log(`value: ${v}`));

// 计数
const numbers = of(1, 2, 3);
numbers
  .pipe(
    scan((total, n) => total + n),
    map((sum, index) => sum / (index + 1)),
  )
  .subscribe(console.log);

// 节流
const clicks1 = fromEvent(document, 'click');
const result1 = clicks1.pipe(throttleTime(1000));
result1.subscribe(x => console.log(x));

// 防抖
const clicks2 = fromEvent(document, 'click');
const result2 = clicks2.pipe(debounceTime(1000));
result2.subscribe(x => console.log(x));
